<%@page import="backpage.model.DBAccessor"%>
<%@page import="backpage.view.PageDivision"%>
<%@page import="backpage.view.ResItem"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	PageDivision pageDivision = (PageDivision) request
			.getAttribute("pageDivision");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="description"
			content="This is the page to show resources">

		<link rel="stylesheet" type="text/css"
			href="<%=basePath%>frontPage/css/resources.css">
		<script type="text/javascript"
			src="<%=basePath%>frontPage/js/resources.js"></script>
		<script type="text/javascript">
		function onSubmit() {
			var page = document.getElementById("curPage").value;
			for(var i=0; i<page.length; i++)
				if( page.charAt(i) <= '0' || page.charAt(i) > '9' ){
					alert("A number is required in the page input box~~");
					return;
				}
			(document.getElementById("mainContent")).action = "<%=basePath%>resources.jsp?resType=<%=request.getParameter("resType")%>";
			(document.getElementById("mainContent")).target = "_self";
			(document.getElementById("mainContent")).submit();
		}
	
		function onSort(sk){
			var curSK = "<%=request.getParameter("sortKey")%>";
			if( curSK == "null" )
				curSK = "0fileName";
			if( curSK.substring(1,curSK.length) == sk ){
				if( curSK.charAt(0) == "0" )
					sk = "1" + sk;
					else sk = "0" + sk;
			}
			else sk = "0" + sk;
			document.getElementById("sortKey").value = sk;
			onSubmit();
		}
		
		function showDescriptions(btn, id, description){
			var descriptions;
			//while(  )
			if( document.getElementById(btn).innerHTML == "显示" ){
				document.getElementById(btn).innerHTML = "隐藏";
				document.getElementById(id).innerHTML = description;
				document.getElementById(id).style.border = "dotted 1px";
				document.getElementById(id).style.margin = "5px 30px 5px 30px";
				document.getElementById(id).style.padding = "5px";
			}else{
				document.getElementById(btn).innerHTML = "显示";
				document.getElementById(id).innerHTML = "";
				document.getElementById(id).style.border = "";				
				document.getElementById(id).style.margin = "0";
				document.getElementById(id).style.padding = "0";
			}
		}
		
		function showDescription(id){
			var btn = document.getElementById("btn"+id).innerHTML;
			if( btn == "" || btn == "显示" ){
				document.getElementById("btn"+id).innerHTML = "隐藏";
				document.getElementById("table"+id).style.display = "";
			}else{
				document.getElementById("btn"+id).innerHTML = "显示";
				document.getElementById("table"+id).style.display = "none";
			}
		}
		
		function onDownload(){
			if( !document.getElementsByName("itemCheckBox") ){
				alert("No item selected~~");
				return;
			}
			var hasItem = false;
			if( document.getElementsByName("itemCheckBox").length ){
				for(  var i=0; i<document.getElementsByName("itemCheckBox").length; i++ ){
					document.getElementsByName("itemCheckBox")[i].value = document.getElementsByName("itemCheckBox")[i].id;	//send the indexes of the checkboxes to the controller
					if( document.getElementsByName("itemCheckBox")[i].checked )
						hasItem = true;
				}
			}
			else{	//only one checkBox
				document.getElementsByName("itemCheckBox").value = document.getElementsByName("itemCheckBox").id;
				if( document.getElementsByName("itemCheckBox").checked )
					hasItem = true;
			}
			if( !hasItem ){
				alert("No item selected~~");
				return;
			}else {
				(document.getElementById("mainContent")).action = "download.servlet?resType=<%=request.getParameter("resType")%>";
				//(document.getElementById("mainContent")).target = "_blank";
				(document.getElementById("mainContent")).submit();
			}
		}
		function onDownloadOne(id){
			(document.getElementById("mainContent")).action = "download.servlet?resType=<%=request.getParameter("resType")%>&resID=" + id;
			//(document.getElementById("mainContent")).target = "_blank";
			(document.getElementById("mainContent")).submit();
		}
	</script>

	</head>

	<body>
		<div>
		<span style="font-family: '微软雅黑'; padding-left:8px; width:100%; color: #F60">
					本网站的资源仅供个人学习使用，不得用于商业用途，本网站并不承担基于此产生的连带责任&nbsp;</span>
			<form id="mainContent" method="post">
				<div id="CntHeader">
					
					<div id="CntHeader" class="left">
						<select name="numInOnePage" id="numInOnePage">
							<!-- option value="10" onClick="onSubmit()">每页显示10个</option-->
							<option value="8" onClick="onSubmit()">
								每页显示8个
							</option>
							<option value="12" onClick="onSubmit()">
								每页显示12个
							</option>
						</select>
						<script type="text/javascript">
		  		  <%String v = request.getParameter("numInOnePage");
			if (v == null)
				v = "8";%>
					document.getElementById("numInOnePage").value = <%=v%>;
				</script>
					</div>

					<div id="CntHeader" class="right">
						<%
							if (pageDivision != null && pageDivision.getCurPage() <= 1) {
						%>
						<a style="cursor: default;"> <img align="absmiddle"
								src="<%=basePath%>frontPage/img/arrow_first_i.gif" /> </a>
						<a style="cursor: default;"> <img align="absmiddle"
								src="<%=basePath%>frontPage/img/arrow_left_i.gif" /> </a>
						<%
							} else {
						%>
						<a onClick="onPage(1,<%=pageDivision.getTotalPageNum()%>)"> <img
								align="absmiddle"
								src="<%=basePath%>frontPage/img/arrow_first.gif" /> </a>
						<a
							onClick="onPage(<%=pageDivision.getCurPage() - 1%>,<%=pageDivision.getTotalPageNum()%>)">
							<img align="absmiddle"
								src="<%=basePath%>frontPage/img/arrow_left.gif" /> </a>
						<%
							}
						%>
						<img align="absmiddle" src="<%=basePath%>frontPage/img/spacer.png" />
						当前页
						<input type="text" style="width: 20px;" name="curPage"
							id="curPage" value="<%=pageDivision.getCurPage()%>" />
						/<%=pageDivision.getTotalPageNum()%>
						<a onClick="onPageInput(<%=pageDivision.getTotalPageNum()%>)">
							<img align="absmiddle" src="<%=basePath%>frontPage/img/go.gif" />
						</a>
						<img align="absmiddle" src="<%=basePath%>frontPage/img/spacer.png" />
						<%
							if (pageDivision != null
									&& pageDivision.getCurPage() >= pageDivision
											.getTotalPageNum()) {
						%>
						<a style="cursor: default;"> <img align="absmiddle"
								src="<%=basePath%>frontPage/img/arrow_right_i.gif" /> </a>
						<a style="cursor: default;"> <img align="absmiddle"
								src="<%=basePath%>frontPage/img/arrow_last_i.gif" /> </a>
						<%
							} else {
						%>
						<a
							onClick="onPage(<%=pageDivision.getCurPage() + 1%>,<%=pageDivision.getTotalPageNum()%>)">
							<img align="absmiddle"
								src="<%=basePath%>frontPage/img/arrow_right.gif" /> </a>
						<a
							onClick="onPage(<%=pageDivision.getTotalPageNum()%>,<%=pageDivision.getTotalPageNum()%>)">
							<img align="absmiddle"
								src="<%=basePath%>frontPage/img/arrow_last.gif" /> </a>
						<%
							}
						%>
					</div>
				</div>
				<div id="CntHeader" class="secondLine">
					<table>
						<tr>
							<td>
								<input type="button" value="批量打包下载" onclick="onDownload()" />
							</td>
						</tr>
					</table>
					<table width="100%">
						<tr>
							<input type="hidden" name="sortKey" id="sortKey" />
							<td width="2%">
								<input type="checkbox" onclick="onAllCheck()" id="allCheck" />
							</td>
							<td width="40%">
								<a onClick="onSort('fileName')" title="单击按文件名排序"> 文件名 <%
									if (request.getParameter("sortKey") == null
											|| request.getParameter("sortKey").equals("0fileName")) {
								%>
									<img src="<%=basePath%>frontPage/img/sortarrowup.gif"
										alt="ascending" /> <%
 	} else if (request.getParameter("sortKey").equals("1fileName")) {
 %>
									<img src="<%=basePath%>frontPage/img/sortarrowdown.gif"
										alt="descending" /> <%
 	}
 %> </a>
							</td>
							<td width="20%">
								<a onClick="onSort('createTime')" title="单击按创建时间排序"> 创建时间 <%
									if (request.getParameter("sortKey") != null
											&& request.getParameter("sortKey").equals("0createTime")) {
								%>
									<img src="<%=basePath%>frontPage/img/sortarrowup.gif"
										alt="ascending" /> <%
 	} else if (request.getParameter("sortKey") != null
 			&& request.getParameter("sortKey").equals("1createTime")) {
 %>
									<img src="<%=basePath%>frontPage/img/sortarrowdown.gif"
										alt="descending" /> <%
 	}
 %> </a>
							</td>
							<td width="15%">
								<a onClick="onSort('touchNum')" title="单击按访问次数排序"> 访问次数 <%
									if (request.getParameter("sortKey") != null
											&& request.getParameter("sortKey").equals("0touchNum")) {
								%>
									<img src="<%=basePath%>frontPage/img/sortarrowup.gif"
										alt="ascending" /> <%
 	} else if (request.getParameter("sortKey") != null
 			&& request.getParameter("sortKey").equals("1touchNum")) {
 %>
									<img src="<%=basePath%>frontPage/img/sortarrowdown.gif"
										alt="descending" /> <%
 	}
 %> </a>
							</td>
							<td width="15%">
								<a onClick="onSort('downloadNum')" title="单击按下载次数排序"> 下载次数
									<%
									if (request.getParameter("sortKey") != null
											&& request.getParameter("sortKey").equals("0downloadNum")) {
								%>
									<img src="<%=basePath%>frontPage/img/sortarrowup.gif"
										alt="ascending" /> <%
 	} else if (request.getParameter("sortKey") != null
 			&& request.getParameter("sortKey").equals("1downloadNum")) {
 %>
									<img src="<%=basePath%>frontPage/img/sortarrowdown.gif"
										alt="descending" /> <%
 	}
 %> </a>
							</td>
							<td width="8%" style="color: #0000FF; font-weight: 600;">
								属性
							</td>
						</tr>
					</table>
					<script type="text/javascript">
				  <%if (request.getParameter("sortKey") == null) {%>
					document.getElementById("sortKey").value = "0fileName";
				  <%} else {%>
					document.getElementById("sortKey").value = "<%=request.getParameter("sortKey")%>";
				  <%}%>
				</script>
				</div>

				<div id="CntMid">
					<%
						Iterator iterator = pageDivision.getitemList().iterator();
						while (iterator.hasNext()) {
							ResItem item = (ResItem) iterator.next();
					%>
					<table width="100%">
						<tr>
							<td width="2%">
								<input type="checkbox" name="itemCheckBox"
									id="<%=item.getID()%>" onClick="onOneCheck()" />
							</td>
							<td width="40%">
								<a onclick="onDownloadOne(<%=item.getID()%>)"> <%=item.getFileName()%></a>
							</td>
							<td width="20%">
								<%=item.getSubmitTime()%>
							</td>
							<td width="15%">
								<%=item.getTouchNum()%>
							</td>
							<td width="15%">
								<%=item.getDownloadNum()%>
							</td>
							<td width="8%">
								<a id="btn<%=item.getID()%>"
									onclick="showDescription(<%=item.getID()%>)">显示</a>
							</td>
						</tr>
					</table>
					<div id="table<%=item.getID()%>"
						style="display: none; margin: 5px 30px 5px 30px; padding: 5px; overflow: hidden; border: dotted 1px #0000FF; color: #0000FF;">
						<%=item.getDescription()%>
					</div>
					<%
						}
					%>
				</div>
				<div id="CntFooter">
				</div>
			</form>
		</div>
	</body>
</html>
